<template>
  <!--   腾讯云上传使用
  1：安装插件
     cos-js-sdk-v5   yarn add  cos-js-sdk-v5
  2:导入
    import Cos from 'cos-js-sdk-v5'
  3:实例化
  let cos = new Cos({
    SecretId: 'COS_SECRETID', // 身份识别 ID
    SecretKey: 'COS_SECRETKEY', // 身份密钥
});
  4:使用
  cos.putObject({
    Bucket: 'dhf481229-1305249343', /* 存储桶: */
    Region: 'ap-guangzhou',     /* 存储桶所在地域，必须字段 */
    Key: uid+'',              /* 必须是字符串 */
    StorageClass: 'STANDARD',  // 固定值
    Body: 文件对象, // 上传文件对象
    onProgress: (progressData)=> {
        上传进度
        console.log(JSON.stringify(progressData));
    }
}, (err, data)=> {
  // 上传成功或者失败的回调
    console.log(err || data);
}); -->
  <div class="uploadImg">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img
        v-if="imageUrl"
        v-imgError="defaultImg"
        :src="imageUrl"
        class="avatar"
        @click.stop="showBigImg"
      >
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>

    <!-- 删除图片 -->
    <div v-if="imageUrl" class="close" @click="closeEvent">X</div>
    <!-- 图片预览 -->
    <el-dialog :visible.sync="isShow" title="图片预览">
      <div style="test-align: center">
        <img class="img" :src="imageUrl" alt="">
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Cos from 'cos-js-sdk-v5'
const cos = new Cos({
  SecretId: 'AKIDhEVVGp79tMYT8XgZBWaNt85mDRO4TbS2',
  SecretKey: 'KODRWMxIMGxObXCbkcuvf8KFtGMBj00i'
})

export default {
  name: 'UploadImg',
  model: {
    prop: 'imageUrl'
  },
  props: {
    imageUrl: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      isShow: false,
      // imageUrl: ''
      defaultImg: require('@/assets/common/head.jpg')
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      // 上传前 限制文件大小与类型
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 自定义
    httpRequest(res) {
      console.log(res)
      // res.file 上传文件对象
      cos.putObject(
        {
          Bucket: 'abc-1308460194' /* 存储桶: */,
          Region: 'ap-chongqing' /* 存储桶所在地域，必须字段 */,
          Key: res.file.uid + '' /* 必须是字符串 */,
          StorageClass: 'STANDARD', // 固定值
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => {
            // 上传进度
            console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          // 上传成功或者失败的回调
          console.log(err || data)
          console.log(data)
          // this.imageUrl = 'http://' + data.Location
          this.$emit('input', 'http://' + data.Location)
        }
      )
    },
    // 删除图片
    closeEvent() {
      this.$emit('input', '')
    },
    // 图片预览
    showBigImg() {
      this.isShow = true
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
::v-deep.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.uploadImg {
  position: relative;
  width: 178px;
  &:hover {
    .close {
      display: block;
    }
  }
  .close {
    display: none;
    position: absolute;
    top: 5px;
    right: 10px;
    cursor: pointer;
  }
  .img {
    width: 100%;
  }
}
</style>
